package com.qisu.command;

import javax.annotation.PostConstruct;

import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import com.qisu.converters.Converters;

import io.netty.channel.ChannelHandlerContext;

/**
 * Hartbeat from pile
 *
 * @author alsan
 *
 */
@Component
@Configuration
public class CMD102 extends AbstractSinexcelCmd {
//	@Autowired HartbeatService _svc;

	@PostConstruct
	@Override
	protected void init() {
		super.init();

		fieldsList.addField(new CommandFieldDef<Integer>(2))
				.addField(new CommandFieldDef<Integer>(2))
				.addField(new CommandFieldDef<String>(32, Converters.strConverter, "充电桩ID"))
				.addField(new CommandFieldDef<Integer>(2, Converters.intConverter, "心跳序列"));
	}

	
	@Override
	protected void processCmd(ChannelHandlerContext ctx, byte[] req) throws Exception {
		responseClient(ctx);
		
	}
	
	
	@Override
	protected void responseClient(ChannelHandlerContext ctx) {
		// cmd=101
		// @formatter:off
		byte[] p = new byte[]{
			(byte)0xAA, (byte)0xF5,			// signature
			(byte)0x00, (byte)0x00, 		// length
			(byte)0x04,						// version
			(byte)0x00,						// sequence
			(byte)0x65, (byte)0x00,			// command
			(byte)0x00, (byte)0x00,			// preserved1
			(byte)0x00, (byte)0x00,			// preserved2
			(byte)0x00, (byte)0x00,			// hartbeat sequence
			(byte)0x00						// checksum
		};

		int seq = ((int)fieldsList.getValue("心跳序列")) + 1;

		p[12] = (byte)(seq & 0xff);
		p[13] = (byte)((seq >> 8) & 0xff);

		setCommand(p, 101);
		send(ctx, p);
	}

}
